//
//  AliPay.swift
//  Aiwei
//
//  Created by zhu xietong on 2018/1/5.
//  Copyright © 2018年 zhu xietong. All rights reserved.
//

import UIKit


enum AliPayResult:Int
{
    case ok = 0
    case canle = -2
    case failed = -1
    case notice_failed = 4
    
}

class FastAliPay: NSObject {
    


    struct Static {
        static var instance:FastAliPay? = nil
    }

    class var  shared:FastAliPay {
        get{
            if Static.instance == nil{
                Static.instance = FastAliPay()
            }
            return Static.instance!
        }
    }

    required override init() {
        super.init()
    }
    
    func dealCallBack(result:NSMutableDictionary)
    {
        print("--nnn====ss=======sdds|\(result)")
        
//        weak var wself = self
        if result["resultStatus","0"] == "9000"
        {
            finish_block(.ok,"支付成功")
            return
        }
        else if result["resultStatus","0"] == "6001"
        {
            self.finish_block(.canle,"支付取消")
            return
        }
        finish_block(AliPayResult.failed,"支付失败")

    }

    var finish_block:((AliPayResult,String)-> Void) = {_,_ in}
    var checkTime:Int = 0
    func paySigner(_ signedString:String,finish_block:@escaping (AliPayResult,String)-> Void)
    {
        
        self.checkTime = 0
        self.finish_block = finish_block

        weak var wself = self
        //        AlipaySDK.defaultService
    
        AlipaySDK.defaultService().payOrder(signedString, fromScheme: SDKConfig.ZifuBao.scheme) { (result:[AnyHashable : Any]?) in
            wself?.checkTime += 1
//            print("--nnn====ss=======sdds|\(result)")
//            if let dict = result?.mutable_dictionary
//            {
//
//                if dict["resultStatus","0"] == "9000"
//                {
//                    if let o_id = UserDefaults.standard.object(forKey: "ly_zfb_order_id")
//                    {
//                        wself?.finish_block(.ok,"支付成功")
//
//                    }
//                    return
//                }
//                else if dict["resultStatus","0"] == "6001"
//                {
//                    wself?.finish_block(.canle,"支付取消")
//                    return
//                }
//                wself?.finish_block(AliPayResult.failed,"支付失败")
//
//            }


        }


    }


    
    
}
